gganimate es una extensión del paquete ggplot2 que permite crear gráficos animados. Permite mostrar cambios en los datos a lo largo del tiempo o de alguna otra dimensión (por ejemplo, categorÃas, etapas, etc.). Las animaciones pueden ser exportadas como GIF, MP4 o HTML (instructor: David Murillo).
library(tidyverse)
library(gganimate)
library(leaflet)
library(maps)
Covid = read.csv("data/covid_19_clean_complete_p1.csv")
Covid = Covid |>
mutate(Fecha = ymd(Date))
Ejemplo: Casos confirmados vs Fecha; PaÃses vs Fecha.
Filtrado de algunos paÃses.
Covid_Filtrado = Covid |>
filter(Country.Region %in% c("Mexico", "Ecuador", "Honduras"))
View(Covid_Filtrado)
head(Covid_Filtrado)
## Province.State Country.Region Lat Long Date Confirmed Deaths
## 1 Ecuador -1.8312 -78.1834 2020-01-22 0 0
## 2 Honduras 15.2000 -86.2419 2020-01-22 0 0
## 3 Mexico 23.6345 -102.5528 2020-01-22 0 0
## 4 Ecuador -1.8312 -78.1834 2020-01-23 0 0
## 5 Honduras 15.2000 -86.2419 2020-01-23 0 0
## 6 Mexico 23.6345 -102.5528 2020-01-23 0 0
## Recovered Active WHO.Region Fecha
## 1 0 0 Americas 2020-01-22
## 2 0 0 Americas 2020-01-22
## 3 0 0 Americas 2020-01-22
## 4 0 0 Americas 2020-01-23
## 5 0 0 Americas 2020-01-23
## 6 0 0 Americas 2020-01-23
ggplot(data = Covid_Filtrado, aes(x = Fecha, y = Confirmed,
color = Country.Region)) +
geom_line()
Guardar gráfico como objeto.
Animado_p1 = ggplot(data = Covid_Filtrado, aes(x = Fecha, y = Confirmed, color = Country.Region )) +
geom_line() +
transition_reveal(Fecha) +
ease_aes("linear")
Animacion_p1 = animate(Animado_p1,
width = 15,
height = 7,
units = "cm",
res = 192,
fps = 20,
duration = 5,
renderer = magick_renderer())
## `geom_line()`: Each group consists of only one observation.
## ℹ Do you need to adjust the group aesthetic?
## `geom_line()`: Each group consists of only one observation.
## ℹ Do you need to adjust the group aesthetic?
Animacion_p1
anim_save("Animacion1.gif", Animacion_p1)
Covid_bar = Covid_Filtrado |>
group_by(Fecha) |>
mutate(rank = rank(-Confirmed)) |>
filter(rank <= 5)
Barra_animado = ggplot(data = Covid_bar,
aes(x = reorder(Country.Region, Confirmed),
y = Confirmed,
fill = Country.Region)) +
coord_flip() +
geom_col() +
transition_time(Fecha) +
ease_aes("linear")
Animacion_p2 = animate(Barra_animado,
width = 15,
height = 7,
units = "cm",
res = 192,
fps = 20,
duration = 10,
renderer = magick_renderer())
Animacion_p2
anim_save("Animacion2.gif", Animacion_p2)
library(leaflet)
Covid_ultima_fecha = Covid |>
filter(Fecha == max(Fecha)) |>
filter(Active > 0)
leaflet::leaflet(Covid_ultima_fecha) |>
leaflet::addTiles() |>
leaflet::addCircleMarkers(
lng = ~Long, lat = ~Lat,
radius = ~sqrt(Active)/100,
color = "red", stroke = FALSE, fillOpacity = 0.5,
popup = ~paste(Country.Region, "<br>Activos:", Active)) |>
leaflet::addLegend("bottomright", colors = "red", labels = "Casos activos", title = "COVID-19")
Covid_filtrado <- Covid %>%
filter(Active > 0)
mundo <- map_data("world")
p_mapa <- ggplot() +
geom_map(data = mundo, map = mundo,
aes(x = long, y = lat, map_id = region),
fill = "gray90", color = "white", size = 0.1) +
geom_point(data = Covid_filtrado,
aes(x = Long, y = Lat, size = Active, frame = Fecha),
color = "red", alpha = 0.5) +
scale_size_continuous(range = c(1, 8), name = "Casos activos") +
labs(title = "Casos Activos de COVID-19",
subtitle = "Fecha: {frame_time}",
x = "", y = "") +
theme_minimal() +
theme(legend.position = "bottom",
axis.text = element_blank(),
panel.grid = element_blank()) +
transition_time(Fecha) +
ease_aes('linear')
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## Warning in geom_map(data = mundo, map = mundo, aes(x = long, y = lat, map_id =
## region), : Ignoring unknown aesthetics: x and y
## Warning in geom_point(data = Covid_filtrado, aes(x = Long, y = Lat, size =
## Active, : Ignoring unknown aesthetics: frame
anim_mapa <- animate(p_mapa,
width = 10,
height = 7,
units = "cm",
res = 192,
fps = 15,
duration = 10,
renderer = gifski_renderer())
anim_mapa
anim_save("MapaAnimado.gif", anim_mapa)